using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Net;

using umbraco;

namespace umbSearch.businessLogic
{
	/// <summary>
	/// Summary description for helper.
	/// </summary>
	public class xsltHelper
	{
		public xsltHelper()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static XslTransform GetXslt(object reference, string XsltFile) 
		{
			if (System.Web.HttpRuntime.Cache["macroXslt_" + XsltFile] != null) 
			{
				return (XslTransform) System.Web.HttpRuntime.Cache["macroXslt_" + XsltFile];
			}
			else 
			{
				XslTransform macroXSLT = new XslTransform();
				XmlTextReader xslReader = new XmlTextReader(System.Web.HttpContext.Current.Server.MapPath(GlobalSettings.Path + "/../xslt/"+XsltFile));
				XmlValidatingReader xslValReader = new XmlValidatingReader( xslReader ); 
				xslValReader.EntityHandling = EntityHandling.ExpandEntities; 
				xslValReader.ValidationType = ValidationType.None; 

				XmlUrlResolver xslResolver = new XmlUrlResolver();
				xslResolver.Credentials = CredentialCache.DefaultCredentials;
				macroXSLT.Load(xslValReader, xslResolver, reference.GetType().Assembly.Evidence);
				xslReader.Close();
				System.Web.HttpRuntime.Cache.Insert(
					"macroXslt_" + XsltFile, 
					macroXSLT, 
					new System.Web.Caching.CacheDependency(System.Web.HttpContext.Current.Server.MapPath(GlobalSettings.Path + "/../xslt/"+XsltFile)));
				return macroXSLT;
			}
		}

	}
}
